<html>
	<head>
		<title>JSEV Extends Test</title>
		<script type="application/javascript" src="../methods.Object.js"></script>
		<script type="application/javascript" src="../methods.Array.js"></script>
		<script type="application/javascript" src="../JSON.schema.reg.js"></script>
		<script type="application/javascript" src="../JSON.path.js"></script>
		<script type="application/javascript" src="../JSON.schema.err.js"></script>
		<script type="application/javascript" src="../JSON.schema.err.messages.en.js"></script>
		<script type="application/javascript" src="../JSON.schema.ext.walker.js"></script>
		<script type="application/javascript" src="../JSON.schema.ext.js"></script>
		<script type="application/javascript">
			function validTest() {
				var schema1 = {},
					schema0 = {"extends": schema1},
					schema3 = {"id": "schema3"},
					schema2 = {"extends": "schema3"},
					schema4 = {"extends": {"$ref" : "schema3"}},
					schemasToValidate = [schema0, schema2, schema4],
					retVal = undefined;

				try {
					JSON.schema.registry.unregister("*");
					JSON.schema.registry.register(schema3);
				} catch (e) {
					console.error("Type registration failed: ", e);
				}

				for (var i in schemasToValidate) {
					retVal = JSON.schema.ext.validate(schemasToValidate[i]);
					if (retVal.valid) {
						console.info("Test " + i + " passed!", retVal);
					} else {
						console.warn("Test " + i + " failed.", retVal);
						if (retVal.errors.length > 0) {
							console.group("Error messages:");
							for (var i in retVal.errors) {
								console.info(JSON.schema.err.localize("en", retVal.errors[i]));
							}
							console.groupEnd();
						} else {
							console.info("No errors reported.");
						}
					}
				}
			}

			function invalidTest() {
				var schema0 = {"extends": "badName"},
					schema1 = {"extends": true},
					schema2 = {"extends": false},
					schema3 = {"extends": undefined},
					schema4 = {"extends": null},
					// Multiple inheritance has been eliminated for the time being
					schema5 = {"extends": []},
					schema6 = {"extends": 23},
					errorsExpected = [
						["IVE000"], ["TE000"], ["TE000"], ["TE000"],
						["TE000"], ["TE000"], ["TE000"]
					],
					schemasToValidate = [
						schema0, schema1, schema2, schema3,
						schema4, schema5, schema6
					],
					retVal = undefined;

				function correctErrors(vRetVal, errorsExpected) {
					if (vRetVal.errors.length != errorsExpected.length)
						return false;

					var i = 0;
					for (; i < errorsExpected.length; i++) {
						if (errorsExpected[i] != vRetVal.errors[i].code) {
							return false;
						}
					}
					return true;
				}

				for (var i in schemasToValidate) {
					retVal = JSON.schema.ext.validate(schemasToValidate[i]);
					if (!retVal.valid && correctErrors(retVal, errorsExpected[i])) {
						console.info("Test " + i + " passed!", retVal);
						if (document.getElementById("showInvMsg").checked) {
							console.group("Error Messages for expected errors:");
							for (var j in retVal.errors)
								console.info(JSON.schema.err.localize("en", retVal.errors[j]));
							console.groupEnd();
						}
					} else {
						console.warn("Test " + i + " failed.", retVal);
						console.group("Reason: ");
						var loops = errorsExpected[i].length > retVal.errors.length ?
							errorsExpected[i].length : retVal.errors.length;
						var msg = "";

						for (var j = 0; j < loops; j++) {
							if (
								j in errorsExpected[i] && j in retVal.errors &&
								errorsExpected[i][j] == retVal.errors[j].code
							) continue;

							if (j in errorsExpected[i])
								msg += "Error Expected: " + errorsExpected[i][j];
							else
								msg += "No error expected";

							if (j in retVal.errors)
								msg += ", found: " + retVal.errors[j].code + ".";
							else
								msg += ", no error found."

							console.info(msg);
							msg = "";
						}
						console.groupEnd();
					}
				}
			}
		</script>
	</head>
	<body>
		<div> Tests the `extends' restriction check of JSON.schema.ext.validate </div>
		<div><button onclick="validTest();">Valid Schema Test</button></div>
		<div><button onclick="invalidTest();">Invalid Schema Test</button><input type="checkbox" id="showInvMsg"></input><label for="showInvMsg">Show Messages</label></div>
	</body>
</html>